﻿function isConnected(connected) {
    if (connected === "True") {
        domains();

        $('#username_TextBox').attr('disabled', 'disabled');
        $('#username_TextBox').attr('style', 'background-color:gray')

        $('#password_TextBox').attr('disabled', 'disabled');
        $('#password_TextBox').attr('style', 'background-color:gray')

        $('#auth_Button').attr('disabled', 'disabled');
        $('#auth_Button').attr('style', 'opacity:0.5');
    }
}

function authenticate() {
    showProgressBar();

    var username = $('#username_TextBox').val();
    var password = $('#password_TextBox').val();
    $.ajax({
        type: "POST",
        url: "/Login/Authenticate",
        dataType: "json",
        data: JSON.stringify({ username: username, password: password }),
        contentType: "application/json; charset=utf-8",
        success: function () {
            if (domains() === false) {
                return;
            }

            $('#username_TextBox').attr('disabled', 'disabled');
            $('#username_TextBox').attr('style', 'background-color:gray')

            $('#password_TextBox').attr('disabled', 'disabled');
            $('#password_TextBox').attr('style', 'background-color:gray')

            $('#auth_Button').attr('disabled', 'disabled');
            $('#auth_Button').attr('style', 'opacity:0.5');
        },
        error: function () {
            alert("Unable to authenticate.");
            return;
        }
    });
    hideProgressBar();
}

function domains() {
    showProgressBar();
    $.ajax({
        type: "GET",
        url: "/Login/Domains",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        acceptHeader: "application/json",
        success: function (data) {
            $.each(data, function (index, value) {
                $('#domainList').append('<option value="' + index + 1 + '">' + value + '</option>');
            });
            $('#domainList').removeAttr("disabled");
        },
        error: function () {
            var result = $('#result').text;
            alert(result);
            return false;
        }
    });
    hideProgressBar();
}

function domains1() {
    showProgressBar();
    $.ajax({
        type: "GET",
        url: "/Login/Domains",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        acceptHeader: "application/json",
        success: function (data) {
            var domains = new Array();
            $.each(data, function (index, value) {
                domains[index] = value;
            });
            return domains;
        },
        error: function () {
            var result = $('#result').text;
            alert(result);
        }
    });
    hideProgressBar();
}

function projects() {
    showProgressBar();
    var selectedDomain = $("#domainList option:selected").text();

    $.ajax({
        type: "POST",
        url: "/Login/Projects",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ domain: selectedDomain }),
        success: function (data) {
            $('#projectList').empty();
            $.each(data, function (index, value) {
                $('#projectList').append('<option value="' + index + 1 + '">' + value + '</option>');
            });
            $('#loginButton').removeAttr("disabled");
            $('#projectList').removeAttr("disabled");
        },
        error: function () {
            var result = $('#result').text;
            alert(result);
        }
    });
    hideProgressBar();
}

function projects1(selectedDomain) {
    showProgressBar();
    $.ajax({
        type: "POST",
        url: "/Login/Projects",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify({ domain: selectedDomain }),
        success: function (data) {
            var projects = new Array();
            $.each(data, function (index, value) {
                projects[index] = value;
            });
        },
        error: function () {
            var result = $('#result').text;
            alert(result);
        }
    });
    hideProgressBar();
}

function login() {
    showProgressBar();
    var domain = $("#domainList option:selected").text();
    var project = $("#projectList option:selected").text();

    $.ajax({
        type: "POST",
        url: "/Login/QCLogin",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        acceptHeader: "application/json",
        data: JSON.stringify({ domain: domain, project: project }),
        success: function () {
            window.location.href = "/Defect/Index/";
        },
        error: function () {
            var result = $('#result').text;
            alert(result);
        }
    });
    hideProgressBar();
}